//电子邮件puhalskijsemen@gmail.com
//源码网站 开vpn全局模式打开 http://web3incubators.com/
//电报https://t.me/gamecode999
//网页客服 http://web3incubators.com/kefu.html

import { GameNetworkManager } from "./0/YpNetMag";
import GameConfig from "./GameConfig";
import { LogMgr } from "./LogManager";
import { SingletonBase } from "./SingletonManager";

/**
 * 建议类名：DataManager
 */
class DataManager extends SingletonBase {
    protected _secret_key: string = "secret-key"; // 建议名称：secretKey
    private _dataMap: Map<string, any> = new Map(); // 建议名称：dataMap

    constructor() {
        super();
    }

    /**
     * 保存数据到本地存储并同步到服务器
     * @param key 数据的键
     * @param value 数据的值
     * 建议方法名：saveData
     */
    protected _save(key: string, value: any): void {
        LogMgr.debug("DataManager", "saveData", key, value);
        LogMgr.debug("YpNetMag.isload=", GameNetworkManager.isLoad);
        if (GameNetworkManager.isLoad) {
            const serializedValue = JSON.stringify(value);
            const storageKey = this._getKey(key);
            this._setKey(storageKey, serializedValue);
            this.saveCount();
            this._dataMap.set(key, value);
            //console.log("本地保存", key, value);
            GameNetworkManager.saveSeverStorage();
        }
    }

    /**
     * 获取密钥
     * @returns 密钥字符串
     * 建议方法名：getSecretKey
     */
    public getSecretKey(): string {
        return this._secret_key;
    }

    /**
     * 获取所有数据
     * @returns 数据映射表
     * 建议方法名：getAllData
     */
    public getAllData(): Map<string, any> {
        return this._dataMap;
    }

    /**
     * 从本地存储加载数据
     * @param key 数据的键
     * @returns 数据的值
     * 建议方法名：loadData
     */
    protected _load(key: string): any {
        const storageKey = this._getKey(key);
        const storedValue = localStorage.getItem(storageKey);
        let parsedValue = null;
        if (storedValue) {
            try {
                parsedValue = JSON.parse(storedValue);
            } catch (error) {
                parsedValue = storedValue;
            }
        }
        this._dataMap.set(key, parsedValue);
        return parsedValue;
    }

    /**
     * 增加保存计数
     * 建议方法名：incrementSaveCount
     */
    protected saveCount(): void {
        let currentVersion = Number(localStorage.getItem("gameversion"));
        if (!currentVersion) {
            currentVersion = 0;
        }
        currentVersion += 1;
        localStorage.setItem("gameversion", currentVersion.toString());
    }

    /**
     * 清除本地存储中的数据
     * @param key 可选，数据的键
     * 建议方法名：clearData
     */
    protected _clear(key?: string): void {
        const keysToClear: string[] = [];
        if (key) {
            keysToClear.push(key);
            for (let i = 0; i < keysToClear.length; i++) {
                const currentKey = keysToClear[i];
                const storageKey = this._getKey(currentKey);
                this._rmKey(storageKey);
            }
        } else {
            localStorage.clear();
        }
    }

    /**
     * 获取存储键
     * @param key 数据的键
     * @returns 存储键
     * 建议方法名：getStorageKey
     */
    protected _getKey(key: string): string {
        if (GameConfig.appConfig.Publish) {
            return `${this._secret_key}-release-${key}`;
        } else {
            return `${this._secret_key}-xfd-${key}`;
        }
    }

    /**
     * 设置存储键值
     * @param key 存储键
     * @param value 存储值
     * 建议方法名：setStorageKey
     */
    protected _setKey(key: string, value: string): void {
        localStorage.setItem(key, value);
    }

    /**
     * 移除存储键
     * @param key 存储键
     * 建议方法名：removeStorageKey
     */
    protected _rmKey(key: string): void {
        localStorage.removeItem(key);
        this._dataMap.delete(key);
    }

    /**
     * 判断值是否为空
     * @param value 要判断的值
     * @returns 是否为空
     * 建议方法名：isNull
     */
    public isNull(value: any): boolean {
        return value == null;
    }
}

export default DataManager;